typedef void EventCallback(arg);

class Event {
  static Map<String, List<EventCallback>> _eventList = {};

  static void on(String eventName, EventCallback f) {
    _eventList[eventName] ??= [];
    _eventList[eventName]!.add(f);
  }

  static void off(String eventName, [EventCallback? f]) {
    List<EventCallback>? list = _eventList[eventName];
    if (list == null) return;
    if (f == null) {
      _eventList.remove(eventName);
    } else {
      list.remove(f);
    }
  }

  static void emit(String eventName, [dynamic arg]) {
    List<EventCallback>? list = _eventList[eventName];
    if (list == null) return;
    list.forEach((EventCallback callback) {
      callback(arg);
    });
  }
}
